package com.bang.sharding.service.impl;

import com.bang.sharding.entity.FixRecords;
import com.bang.sharding.dao.FixRecordsDao;
import com.bang.sharding.service.FixRecordsService;
import io.shardingsphere.api.HintManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 公有关键词修复记录表(FixRecords)表服务实现类
 *
 * @author makejava
 * @since 2020-04-08 18:09:00
 */
@Service("fixRecordsService")
@Slf4j
public class FixRecordsServiceImpl implements FixRecordsService {
    @Resource
    private FixRecordsDao fixRecordsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public FixRecords queryById(Long id) {
        return this.fixRecordsDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<FixRecords> queryAllByLimit(int offset, int limit) {
        return this.fixRecordsDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param fixRecords 实例对象
     * @return 实例对象
     */
    @Transactional
    @Override
    public FixRecords insert(FixRecords fixRecords) {
        this.fixRecordsDao.insert(fixRecords);
        return fixRecords;
    }

    /**
     * 修改数据
     *
     * @param fixRecords 实例对象
     * @return 实例对象
     */
    @Override
    public FixRecords update(FixRecords fixRecords) {
        this.fixRecordsDao.update(fixRecords);
        return this.queryById(fixRecords.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.fixRecordsDao.deleteById(id) > 0;
    }

    @Override
    public FixRecords insertAndUpdate(FixRecords fixRecords) {
         this.fixRecordsDao.insert(fixRecords);
//        HintManager.getInstance().setMasterRouteOnly();
//        FixRecords fixRecords1 = this.queryById(fixRecords.getId());
//        log.info("Result={}", fixRecords1);
        fixRecords.setTemplateId(10086L);
        fixRecords.setCreateTime(new Date());
        FixRecords update = this.update(fixRecords);
        return update;
    }
}